package cn.founder.org.service.impl;

import cn.founder.basic.service.impl.BaseServiceImpl;
import cn.founder.basic.util.PageList;
import cn.founder.org.domain.Department;
import cn.founder.org.mapper.DepartmentMapper;
import cn.founder.org.query.DepartmentQuery;
import cn.founder.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        //传入时候 没有dirpath
        // 那么就需要将自增后现有id传回 用作将自己id拼接写入dirpath
        departmentMapper.save(department);
        this.update(department);
    }


    @Override
    @Transactional
    public void update(Department department) {
        //封装顶级部门写入
        String dirPath = "";
        // 1.传入部门的顶级部门
        if(department.getParent()==null){
            //传入时如果父级部门为空  那么就把自己的id拼接存入dirpath 为顶级
            //department.setDirPath("/"+department.getId());
            dirPath = "/"+department.getId();
            //写入 dirpath
        }else {
            // 2.传入非顶级部门
            //前端选择了父部门  那么必定有id 根据id查询相应的部门
            Department parentDept = departmentMapper.loadById(department.getParent().getId());
            //将查到的父级的dirpath 拼接到前面   后面是自己的/id
            //department.setDirPath(parentDept.getDirPath() + "/" +department.getId())
            dirPath = parentDept.getDirPath() + "/" +department.getId();
        }
        //将拼接好的字符串存入对象  然后再使用方法写入dirpath
        department.setDirPath(dirPath);
        departmentMapper.update(department);

    }


    //获得部门树
    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }

    //独特的实现需要自己单独写
}
